local game = require "club.threecard.blackjack"

local banker = game.ui.element()

local log = game.logging.get("banker")

banker.store_watchers = 
{
    ingame = 
    {
        ["阶段变更"] = function(self, store, state, args, initialized)
            if initialized then
                self:hide_all_pos();
                self:play(1, "daiji", true);
                return;
            end

            if state.phase ~= state.PHASE.PLAYER then
                if state.phase == state.PHASE.INSURE_RESULT then
                    game.gamelib.coroutine.start(function()
                        self:play(1, "mipai01", false);
                        self:play_youshou(false);
                        game.gamelib.coroutine.wait(0.4);
                        self:play(1, "mipai", true);
                        self:play_youshou(false);
                        game.gamelib.coroutine.wait(1);
                        local croupier = game.require "ui.ingame.croupier"
                        croupier.tip(state.tip_insure_result, 2.5);
                        self:play(1, "daiji", true);
                        self:play_youshou(false);
                    end)
                else
                    self:play(1, "daiji", true);
                    self:play_youshou(false);
                end
            end
        end,

        ["庄家展示底牌"] = function(self, store, state, card_id, initialized)
            if initialized then
                return;
            end

            local hand = self.poker_container.hand;
            for _, poker in ipairs(hand) do
                if poker.data.client_value < 0 then
                    local poker_data = self.poker_container.config.poker_data_class.new(card_id)
                    poker:flip(poker_data, function()
                        self.poker_container:confirm_amount();
                    end);
                    break;
                end
            end
        end,

        ["玩家回合"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end

            self:update_motion(state)
        end
    }
}

function banker:ctor()
    self.res = "club.threecard.blackjack/ui/ingame.b:banker"
    local config = game.require "poker_controller.poker_container_config"
    config = config.banker_config
    local container = game.require "poker_controller.poker_container"
    self.poker_container = container.new(config, -1);
end

function banker:on_injected(bind)
    self.poker_container:set_gameobject(self.poker_container_obj, true);
    self.poker_container:clear();
end

-- 播放动作
function banker:play(pose_id, anim, loop, time_scale)
    time_scale = 1 or 1;
    loop = loop or false;
    if self.pose_id ~= pose_id then
        local current_pose_anim = self["pose_"..tostring(self.pose_id)];
        if current_pose_anim then
           current_pose_anim.gameObject:SetActive(false); 
        end
        self.pose_id = pose_id
        current_pose_anim = self["pose_"..tostring(self.pose_id)];

        if current_pose_anim then
            current_pose_anim.gameObject:SetActive(true);
        end
    end

    self["pose_"..tostring(self.pose_id)].loop = loop;
    self["pose_"..tostring(self.pose_id)].AnimationName = anim;
    self["pose_"..tostring(self.pose_id)].timeScale = time_scale;
end

function banker:play_youshou(b)
    self.pose_3.gameObject:SetActive(b);
    if not b then
        return;
    end

    self.pose_3.AnimationName = "youshou1";
    self.pose_3.loop = false;
end

function banker:hide_all_pos()
    self.pose_1.gameObject:SetActive(false);
    self.pose_2.gameObject:SetActive(false);
    self.pose_3.gameObject:SetActive(false);
end

function banker:update_motion(state)
    if state.current_player == nil then
        self:play(1, "daiji", true)
        self:play_youshou(false);
        return;
    end

    local be_split = false;
    for _, id in pairs(state.action_ids) do
        if id == 2 then
            be_split = true;
            break;
        end
    end

    local no = state.current_player.seat.no;
    self:play_youshou(true);
    if be_split then
        game.gamelib.log.debug(string.format("fenpai%s01", no*2-1));
        --self:play(2, string.format("fenpai%s01", no*2-1), false);
        -- 可以分牌 指过去的动画 应该是两个手指那个
        self:play(2, string.format("zhipai%s01", no));
    else
        game.gamelib.log.debug(tostring(state.current_player_splitted));
        if state.current_player_splitted then
            if state.current_active_hand_index == 0 then
                game.gamelib.log.debug(string.format("fenpai%s03", no*2-1));
                self:play(2, string.format("fenpai%s03", no*2-1));
            else
                game.gamelib.log.debug(string.format("fenpai%s03", no*2));
                self:play(2, string.format("fenpai%s03", no*2));
            end
        else
            game.gamelib.log.debug(string.format("zhipai%s01", no));
            self:play(2, string.format("zhipai%s01", no));
        end
    end
end

function banker:resume(card_ids)
    self.poker_container:clear();
    self.poker_container:fill(card_ids);
    self.poker_container:update_poker_amount();
    self:update_motion(game.store.ingame.state)
end

function banker:clear_scene()
    self.poker_container:clear()
    self:play(1, "daiji", true);
    self:play_youshou(false);
end

return banker